<?php namespace Core\Myapp\Helper;

class Url extends \Core\Myapp\Zobero
{

    public function getBaseUrl($secure = false)
    {
        $base_url = '';
        if (isset($_SERVER['HTTP_HOST']) && preg_match('/^((\[[0-9a-f:]+\])|(\d{1,3}(\.\d{1,3}){3})|[a-z0-9\-\.]+)(:\d+)?$/i', $_SERVER['HTTP_HOST']))
        {
            $base_url = (($secure==true) ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST']
                .substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME'])));
        }
        else
        {
            $base_url = 'http://localhost/';
        }

        return $base_url;
    }
    public function getAdminUrl()
    {
        if($this->getRegistry('router')->getArea()=='frontend')
            return false;
        else
            return $this->getBaseUrl() . $this->getRegistry('router')->getAdminRoute() .'/';
    }
    public static function getProductUrl($product)
    {

    }
    public static function getCategoryUrl($category)
    {

    }
    public static function getPageUrl($indentifier)
    {

    }
    public function getFrontendThemeUrl($secure)
    {
        $baseUrl = $this->getBaseUrl($secure);
        return $baseUrl . 'app/design/frontend/' . \Registry::get('bootstrap')->getDesignPackage();
    }
    public function getBackendThemeUrl($secure)
    {
        $baseUrl = $this->getBaseUrl($secure);
        return $baseUrl . 'app/design/backend/theme/default';
    }
    protected function getFrontendCssUrl($path)
    {
        $themeUrl = $this->getFrontendThemeUrl(false);
        return $themeUrl . '/css/' . trim($path, '/');
    }
    protected function getBackendCssUrl($path)
    {
        $themeUrl = $this->getBackendThemeUrl(false);
        return $themeUrl . '/css/' . trim($path, '/');
    }
    protected function getFrontendJsUrl($path)
    {
        $themeUrl = $this->getFrontendThemeUrl(false);
        return $themeUrl . '/js/' . trim($path, '/');
    }
    protected function getBackendJsUrl($path)
    {
        $themeUrl = $this->getBackendThemeUrl(false);
        return $themeUrl . '/js/' . trim($path, '/');
    }
    public function getCssUrl($path)
    {
        if($this->getRegistry('router')->getArea()=='admin')
            return $this->getBackendCssUrl($path);
        else
            return $this->getFrontendCssUrl($path);
    }
    public function getJsUrl($path)
    {
        if($this->getRegistry('router')->getArea()=='admin')
            return $this->getBackendJsUrl($path);
        else
            return $this->getFrontendJsUrl($path);
    }
    public function buildUrl($route, $controller = 'index', $action = 'index', $params = array())
    {
        $uri = $route . '/' . $controller . '/' . $action;
        foreach($params as $key=>$value) {
            $uri .= '/' . $key . '/' . $value;
        }
        $uri .= '/';
        $admin = '';
        if($this->getRouter()->getArea()=='admin')
            $admin = 'admin/';
        return $this->getBaseUrl() . $admin . $uri;
    }
    public function buiUrlFromUri($url)
    {
        $uri = trim($url, '/');
        $uri = explode('/', $uri);
        return $this->buildUrl($uri[0], $uri[1], $uri[2]);
    }
    public function filterUrl($string)
    {
        preg_match('/{.*}/', $string, $matches);
        if(!$matches)
            return $string;
        $uri = trim(trim($matches[0],'{'), '}');
        $url = $this->buiUrlFromUri($uri);
        return preg_replace('/{.*}/', $url, $string);
    }
}